package com.bit.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bit.blog.constant.Constants;
import com.bit.blog.mapper.BlogInfoMapper;
import com.bit.blog.pojo.dataobject.BlogInfo;
import com.bit.blog.pojo.request.AddBlogRequest;
import com.bit.blog.pojo.request.UpdateBlogRequest;
import com.bit.blog.pojo.response.BlogDetailResponse;
import com.bit.blog.pojo.response.BlogInfoResponse;
import com.bit.blog.service.BlogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("blogService")
public class BlogServiceImpl implements BlogService {

    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.NOT_DELETE);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);

        List<BlogInfoResponse> blogInfoResponseList = blogInfos.stream().map(blogInfo -> {
            BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
            BeanUtils.copyProperties(blogInfo, blogInfoResponse);
            return blogInfoResponse;
        }).collect(Collectors.toList());
        return blogInfoResponseList;
    }

    @Override
    public BlogDetailResponse getBlogDetail(Integer userId) {
        BlogDetailResponse blogDetailResponse = new BlogDetailResponse();
        BlogInfo blogInfo = blogInfoMapper.selectById(userId);
        BeanUtils.copyProperties(blogInfo, blogDetailResponse);
        return blogDetailResponse;
    }

    @Override
    public Boolean addBlog(AddBlogRequest blogInfoRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(blogInfoRequest, blogInfo);
        try{
            int insert = blogInfoMapper.insert(blogInfo);
            if(insert == 1){
                return true;
            }
        }catch (Exception e){
            log.error("添加博客失败, e:",e);
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(updateBlogRequest, blogInfo);
        try{
            int ret = blogInfoMapper.updateById(blogInfo);
//            int ret = blogInfoMapper.update(new UpdateWrapper<>(blogInfo));
            if(ret >= 1) {
                return true;
            }
        }catch (Exception e){
            log.error("编辑博客失败, e:",e);
        }

        return false;
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        try{
            int ret = blogInfoMapper.update(new LambdaUpdateWrapper<BlogInfo>()
                    .set(BlogInfo::getDeleteFlag, Constants.IS_DELETE)
                    .eq(BlogInfo::getId, blogId));
            if(ret >= 1){
                return true;
            }
        }catch (Exception e){
            log.error("删除博客失败，e:",e);
        }

        return false;
    }
}
